package www.yingdi365.com.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpHeaders;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;
import www.yingdi365.com.R;
import www.yingdi365.com.base.ZjbBaseActivity;
import www.yingdi365.com.constant.Constant;
import www.yingdi365.com.model.AppMicrofacies;
import www.yingdi365.com.model.AppMicrofaciesPaging;
import www.yingdi365.com.model.OkObject;
import www.yingdi365.com.util.GsonUtils;
import www.yingdi365.com.util.LogUtil;

public class XuanZeZPActivity extends ZjbBaseActivity implements View.OnClickListener {

    private GridView gridView;
    private List<AppMicrofacies.PictureListBean> pictureList = new ArrayList<>();
    private MyAdapter adapter;
    private int index;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xuan_ze_zp);
        init();
    }

    @Override
    protected void initSP() {

    }

    @Override
    protected void initIntent() {
        Intent intent = getIntent();
        index = intent.getIntExtra(Constant.INTENT_KEY.Position, -1);
    }

    @Override
    protected void findID() {
        gridView = (GridView) findViewById(R.id.gridView);
    }

    @Override
    protected void initViews() {
        ((TextView) findViewById(R.id.textViewTitle)).setText("选择图片");
        adapter = new MyAdapter();
        gridView.setAdapter(adapter);
    }

    @Override
    protected void setListeners() {
        findViewById(R.id.imageBack).setOnClickListener(this);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.putExtra(Constant.INTENT_KEY.PictureListBean, pictureList.get(position));
                intent.putExtra(Constant.INTENT_KEY.Position,index);
                setResult(Constant.REQUEST_RESULT_CODE.IMAGE_PICKER, intent);
                finish();
            }
        });
    }

    private OkObject getOkObject() {
        String url = Constant.HOST + Constant.Url.AppMicrofaciesService + "query" + "?page=0" + "&filter=&sort_key=isAutoGeneration&desc=true&page_size=100";
        HttpHeaders headers = new HttpHeaders();
        headers.put(Constant.TOKEN, appLoginResult.getToken());
        HashMap<String, String> params = new HashMap<>();
        return new OkObject(headers, params, url);
    }

    @Override
    protected void initData() {
        showLoadingDialog();
        OkObject okObject = getOkObject();
        OkGo.post(okObject.getUrl())//
                .tag(this)//
                .headers(okObject.getHeaders())
                .upJson(okObject.getJson())//
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        cancelLoadingDialog();
                        LogUtil.LogShitou("XuanZeZPActivity--onSuccess", "");
                        AppMicrofaciesPaging appMicrofaciesPaging = GsonUtils.parseJSON(s, AppMicrofaciesPaging.class);
                        List<AppMicrofacies> microfaciesBeanList = appMicrofaciesPaging.getMicrofacies();
                        for (int i = 0; i < microfaciesBeanList.size(); i++) {
                            pictureList.addAll(microfaciesBeanList.get(i).getPictureList());
                        }
                        adapter.notifyDataSetChanged();
                        try {
                        } catch (Exception e) {
                            Toast.makeText(XuanZeZPActivity.this, getResources().getString(R.string.shujuchucuo), Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        cancelLoadingDialog();
                        if (response != null) {
                            if (response.code() == 401) {
                                showReLoginDialog();
                            }
                        }
                        Toast.makeText(XuanZeZPActivity.this, getResources().getString(R.string.qingqiushibai), Toast.LENGTH_SHORT).show();
                    }
                });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imageBack:
                finish();
                break;
        }
    }

    class MyAdapter extends BaseAdapter {
        class ViewHolder {
            public ImageView image;
        }

        @Override
        public int getCount() {
            return pictureList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(XuanZeZPActivity.this).inflate(R.layout.item_bian_ji_wxb, null);
                holder.image = (ImageView) convertView.findViewById(R.id.image);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            Glide.with(XuanZeZPActivity.this)
                    .load(pictureList.get(position).getUrl())
                    .placeholder(R.mipmap.ic_empty)
                    .into(holder.image);
            return convertView;
        }
    }
}
